package com.ticketbooking.service.impl;

import com.ticketbooking.entity.RouteInfEntity;
import com.ticketbooking.service.IRouteInfDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Types;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RouteInfDaoImpl implements IRouteInfDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> getRouteInfByRouteId(String routeid) {
        String sql = "select * from routeinf u where u.routeid= ?";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{routeid},new int[]{Types.VARCHAR});
        return list;

    }
    @Override
    public int UpdateRouteInf(RouteInfEntity routeinf) {
        String  routeid = routeinf.getRouteid();
        String sql ="UPDATE routeinf set airline=?,airtype=?,departure=?,destination=?,depairport=?,desairport=?,deptime=?,destime=?,vipprice=?,norprice=?,remarks=? where routeid='"+routeid+"'";
        Object args[] = {routeinf.getAirline(),routeinf.getAirtype(),routeinf.getDeparture(),routeinf.getDestination(),routeinf.getDepairport(),routeinf.getDesairport(),routeinf.getDeptime(),routeinf.getDestime(),routeinf.getVipprice(),routeinf.getNorprice(),routeinf.getRemarks()};
        int result = jdbcTemplate.update(sql,args);
        return result;
    }

    @Override
    public List getRouteInfByDepTime(String beginTime, String desTime) {
        String sql = "select * from routeinf  where deptime > ? and destime < ? ";
        List<Map<String, Object>> routeinflist = jdbcTemplate.queryForList(sql,new Object[]{beginTime,desTime},new int[]{Types.VARCHAR,Types.VARCHAR});
        return routeinflist;
    }

    @Override
    public List getRouteInfByPort(String depairport, String desairport) {
        String sql = "select * from routeinf where depairport= ? and desairport = ? ";
        List<Map<String, Object>> routeinflist = jdbcTemplate.queryForList(sql,new Object[]{depairport,depairport},new int[]{Types.VARCHAR,Types.VARCHAR});
        return routeinflist;
    }
    @Override
    public List getRouteInf() {
        String sql = "select * from routeinf ";
        List<Map<String, Object>> routeinflist = jdbcTemplate.queryForList(sql);
        System.out.println(routeinflist.get(0).get("deptime").getClass());
        return routeinflist;
    }

    @Override
    public void save(RouteInfEntity routeinf){
        String sql = "insert into routeinf(routeid,airline,airtype,depairport,desairport,departure,destination,deptime,destime,vipprice,norprice,remarks) values (?,?,?,?,?,?,?,?,?,?,?,?)";
        Object args[] = {routeinf.getRouteid(),routeinf.getAirline(),routeinf.getAirtype(),routeinf.getDepairport(),routeinf.getDesairport(),routeinf.getDeparture(),routeinf.getDestination(),routeinf.getDeptime(),routeinf.getDestime(),routeinf.getVipprice(),routeinf.getNorprice(), routeinf.getRemarks()};
//        int[] argTypes = {Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,Types.TIMESTAMP,Types.TIMESTAMP,Types.VARCHAR,Types.VARCHAR,Types.VARCHAR};
        jdbcTemplate.update(sql,args);


    }


    private RouteInfEntity getRouteInf( List<Map<String, Object>> routelist) {
        RouteInfEntity routeinf = new RouteInfEntity();
        if (routelist.size() > 0) {
            Map<String, Object> map = routelist.get(0);
            routeinf.setRouteid((String) map.get("routeid"));
            routeinf.setAirline((String) map.get("airline"));
            routeinf.setAirtype((String) map.get("airtype"));
            routeinf.setDepairport((String) map.get("depairport"));
            routeinf.setDeparture((String) map.get("departure"));
            routeinf.setDeptime((String) map.get("deptime"));
            routeinf.setDestime((String) map.get("destime"));
            routeinf.setVipprice((String) map.get("vipprice"));
            routeinf.setNorprice((String) map.get("norprice"));
            routeinf.setRemarks((String) map.get("remarks"));
        }
        return routeinf;
    }
}
